package com.xyz.util;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tongqiang
 * @description
 * @date 2022年07月21日
 */
public class WordTemplateUtil {


    public static void exportWord(HttpServletResponse response, Map<String, Object> dataMap, String fileName) throws Exception {

        // 将文件名重新编码，解决浏览器下载文件名乱码问题
        String reCodeFileName = StrUtil.str(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=".concat(reCodeFileName));
        ClassPathResource classPathResource = new ClassPathResource("doc/" + fileName);
        String path = classPathResource.getPath();
        XWPFDocument document = WordExportUtil.exportWord07(path, dataMap);
        try (ServletOutputStream out = response.getOutputStream();

        ) {
            document.write(out);
            out.flush();
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }


    //String转空格
    public static String nullToString(String str) {
        if (str == null||str.equals("")) {
            return " ";
        }
        return str;
    }

    public static String parse(String dateArg, DateTimeFormatter sourceFormatter, DateTimeFormatter targetFormatter) {
        if (StrUtil.isNotBlank(dateArg))
            return LocalDateTime.parse(dateArg, sourceFormatter).format(targetFormatter);
        return " ";
    }

    public static String parseTime(String dateArg, DateTimeFormatter sourceFormatter, DateTimeFormatter targetFormatter) {
        if (StrUtil.isNotBlank(dateArg))
            return LocalDateTime.parse(dateArg, sourceFormatter).format(targetFormatter);
        return " ";
    }


    /**
     * @return 切割字符串
     */
    //"肖钰|1D7EB549206A201AE0530AE40381201A,李享|1D7EB54920A2201AE0530AE40381201A,袁琦|1D7EB54920BA201AE0530AE40381201A,彭思远|55A44DF17CCC945EE0530AE40713945E,廖政洁|a81e53ed31f14befbc650f789b058f8d,贾献洲|E49BAD123286D006E0430AE40943D006,叶军华|E49BAD1241C2D006E0430AE40943D006,高耀刚|E49BAD1241D1D006E0430AE40943D006,朱晓华|E49BAD124201D006E0430AE40943D006";
    public static String getSplice(String str) {
        if (StrUtil.isNotBlank(str)) {
            String[] split = str.split(",");
            StringBuilder sb = new StringBuilder();
            for (String s : split) {
                if (s.contains("|")) {
                    String substring = s.substring(0, s.indexOf("|"));
                    sb.append(substring).append(",");
                } else {
                    sb.append(s).append(",");
                }
            }
            return sb.toString().substring(0, sb.toString().length() - 1);
        }
        return " ";
    }


    /**
     * @param gzrw
     * @param key_01
     * @param key_02
     * @return
     */

    public static List<Map<String, Object>> getList(List<String> gzrw, String key_01, String key_02) {
        List<Map<String, Object>> list = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(gzrw)) {
            for (String s : gzrw) {
                String[] split = s.split("|");
                Map<String, Object> map = new HashMap<>();
                map.put(key_01, split[0]);
                map.put(key_02, split[1]);
                list.add(map);
            }
        }
        return list;
    }


    public static void main(String[] args) {
        String str = "肖钰|1D7EB549206A201AE0530AE40381201A,李享|1D7EB54920A2201AE0530AE40381201A,袁琦|1D7EB54920BA201AE0530AE40381201A,彭思远|55A44DF17CCC945EE0530AE40713945E,廖政洁|a81e53ed31f14befbc650f789b058f8d,贾献洲|E49BAD123286D006E0430AE40943D006,叶军华|E49BAD1241C2D006E0430AE40943D006,高耀刚|E49BAD1241D1D006E0430AE40943D006,朱晓华|E49BAD124201D006E0430AE40943D006";
        String splice = getSplice(str);
        System.out.println(splice);
    }


}
